package com.bcbim.api_web.table_head.controller;

import com.bcbim.authentication_interface.util.AuthUtil;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.sys.Constant;
import com.bcbim.common.util.GuidUtil;
import com.bcbim.operation_maintenance_interface.table_head.dao.SpaceDepartmentTableHeadDAO;
import com.bcbim.operation_maintenance_interface.table_head.dto.TableCheckDTO;
import com.bcbim.operation_maintenance_interface.table_head.service.SpaceDepartmentTableHeadService;
import com.bcbim.operation_maintenance_interface.table_head.vo.SpaceDepartmentTableHeadVO;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V 1.0.0
 * @Description：业务部门表头设置controller
 * @Author: haomeijie
 * @Date: 2022/3/9 10:01
 **/
@RestController
@RequestMapping("/spaceDepartmentTable")
public class SpaceDepartmentTableHeadController {
    @Reference(protocol = "dubbo", group = "spaceDepartment", version = "1.0.0")
    private SpaceDepartmentTableHeadService spaceDepartmentTableHeadServiceImpl;

    /**
     * 空间信息表头设置列表
     *
     * @Author: haomeijie
     * @Date: 2022/3/9 10:13
     */
    @RequestMapping("/list")
    public ReturnMsg listSpaceTableHead(TableCheckDTO tableCheckDTO) {
        //判断用户是否设置过表头
        tableCheckDTO.setUserId(AuthUtil.getUserIdDecrypt());
        boolean exit = spaceDepartmentTableHeadServiceImpl.checkTableHeadByUserId(tableCheckDTO);
        if (!exit) {
            //如果不存在，查询模板
            tableCheckDTO.setUserId(null);
            tableCheckDTO.setProjectId(null);
        }
        //查询用户设置过的
        List<SpaceDepartmentTableHeadVO> list = spaceDepartmentTableHeadServiceImpl.listTableHead(tableCheckDTO);
        return ReturnMsg.consSuccess(list);
    }

    /**
     * 保存表头设置
     *
     * @param tableCheckDTO
     * @return ReturnMsg
     * @Author: haomeijie
     * @Date: 2022/3/9 16:38
     */
    @RequestMapping("/save")
    @Transactional
    public ReturnMsg saveSpaceTableHead(TableCheckDTO tableCheckDTO) {
        //判断用户是否设置过表头
        tableCheckDTO.setUserId(AuthUtil.getUserIdDecrypt());
        boolean exit = spaceDepartmentTableHeadServiceImpl.checkTableHeadByUserId(tableCheckDTO);
        if (exit) {
            //进行设置修改
            List<SpaceDepartmentTableHeadDAO> list = new ArrayList<>();
            for (String id : tableCheckDTO.getIds()) {
                SpaceDepartmentTableHeadDAO spaceDepartmentTableHeadDAO = new SpaceDepartmentTableHeadDAO();
                spaceDepartmentTableHeadDAO.setId(id)
                        .setCheckType(Constant.CHECK);
                list.add(spaceDepartmentTableHeadDAO);
            }
            //修改勾选的
            int i = spaceDepartmentTableHeadServiceImpl.batchUpdateTableHead(list);
            //取消未勾选的
            spaceDepartmentTableHeadServiceImpl.batchNoChecked(tableCheckDTO);
            return i > 0 ? ReturnMsg.consSuccess(Constant.SAVE_SUCCESS) : ReturnMsg.consFail(Constant.SAVE_FAIL);
        }
        List<SpaceDepartmentTableHeadVO> list = spaceDepartmentTableHeadServiceImpl.listTableHead(new TableCheckDTO());
        List<SpaceDepartmentTableHeadDAO> headList = new ArrayList<>();
        for (SpaceDepartmentTableHeadVO spaceDepartmentTableHeadVO : list) {
            SpaceDepartmentTableHeadDAO spaceDepartmentTableHeadDAO = new SpaceDepartmentTableHeadDAO();
            //复制属性
            BeanUtils.copyProperties(spaceDepartmentTableHeadVO, spaceDepartmentTableHeadDAO);
            spaceDepartmentTableHeadDAO.setId(GuidUtil.get())
                    .setUserId(AuthUtil.getUserIdDecrypt())
                    .setProjectId(tableCheckDTO.getProjectId())
                    .setDel(Constant.NOT_DEL);
            //根据传值判断哪些勾选
            for (String id : tableCheckDTO.getIds()) {
                if (id.equals(spaceDepartmentTableHeadVO.getId())) {
                    spaceDepartmentTableHeadDAO.setCheckType(Constant.CHECK);
                }
            }
            headList.add(spaceDepartmentTableHeadDAO);
        }
        int j = spaceDepartmentTableHeadServiceImpl.batchInsertTableHead(headList);
        return j > 0 ? ReturnMsg.consSuccess(Constant.SAVE_SUCCESS) : ReturnMsg.consFail(Constant.SAVE_FAIL);
    }
}
